package com.pinyougou.shop.controller;
import java.util.List;

import com.pinyougou.pojogroup.GoodsAndDesc;
import entity.ResultInfo;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbGoods;
import com.pinyougou.sellergoods.service.GoodsService;

import com.github.pagehelper.PageInfo;
/**
 * controller
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

	@Reference
	private GoodsService goodsService;
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<TbGoods> findAll(){			
		return goodsService.findAll();
	}
	
	
//	/**
//	 * 返回全部列表
//	 * @return
//	 */
//	@RequestMapping("/findPage")
//	public PageInfo<TbGoods>  findPage(int page,int rows){
//		return goodsService.findPage(page, rows);
//	}
	
	/**
	 * 增加,商品录入
	 * @param goodsAndDesc
	 * @return
	 */
	@RequestMapping("/add")
	public ResultInfo add(@RequestBody GoodsAndDesc goodsAndDesc){

		//获取登录名
		String sellerId = SecurityContextHolder.getContext().getAuthentication().getName();

		goodsAndDesc.getGoods().setSellerId(sellerId); //设置商家id;
		try {
			goodsService.add(goodsAndDesc);
			return new ResultInfo(true, "增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultInfo(false, "增加失败");
		}
	}

	/**
	 * 修改
	 * @param goodsAndDesc
	 * @return
	 */
	@RequestMapping("/update")
	public ResultInfo update(@RequestBody GoodsAndDesc goodsAndDesc){
		try {
			goodsService.update(goodsAndDesc);
			return new ResultInfo(true, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultInfo(false, "修改失败");
		}
	}	
	
	/**
	 * 获取实体
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public GoodsAndDesc findOne(Long id){
		return goodsService.findOne(id);		
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public ResultInfo delete(Long [] ids){
		try {
			goodsService.delete(ids);
			return new ResultInfo(true, "删除成功"); 
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultInfo(false, "删除失败");
		}
	}
	
		/**
	 * 查询+分页
	 * @param goods
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageInfo<TbGoods> search(@RequestBody TbGoods goods, int page, int rows  ){

	    //获取商家id
        String sellerId = SecurityContextHolder.getContext().getAuthentication().getName();

        goods.setSellerId(sellerId);
        return goodsService.findPage(goods, page, rows);
	}
	
}
